package com.jilian.mydemo.modul.my.ui;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jilian.mydemo.Constant;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.modul.main.ui.MainActivity;
import com.jilian.mydemo.modul.user.activity.LoginActivity;
import com.jilian.mydemo.utils.CleanDataUtils;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.PinziDialogUtils;
import com.jilian.mydemo.utils.RxTimerUtil;
import com.jilian.mydemo.utils.SPUtil;
import com.jilian.mydemo.utils.ToastUitl;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * 设置
 */
public class SettingActivity extends BaseActivity {
    private RelativeLayout rlOne;
    private RelativeLayout rlTwo;
    private RelativeLayout rlThree;
    private TextView tvSize;
    private TextView tvOk;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        setNormalTitle("设置", v -> finish());
        rlOne = (RelativeLayout) findViewById(R.id.rl_one);
        rlTwo = (RelativeLayout) findViewById(R.id.rl_two);
        rlThree = (RelativeLayout) findViewById(R.id.rl_three);
        tvSize = (TextView) findViewById(R.id.tv_size);
        tvOk = (TextView) findViewById(R.id.tv_ok);
    }

    @Override
    public void initData() {
        try {
            tvSize.setText(CleanDataUtils.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void initListener() {
        rlOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this,UpdatePhoneActivity.class));
            }
        });
        rlTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this,UpdatePwdActivity.class));
            }
        });
        rlThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    CleanDataUtils.clearAllCache(SettingActivity.this);
                    ToastUitl.showImageToastSuccess("清理成功");
                    tvSize.setText(CleanDataUtils.getTotalCacheSize(SettingActivity.this));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showLogoutDialog();
            }
        });


    }

    /**
     * 退出登录对话框
     */
    private void showLogoutDialog() {
        Dialog dialog = PinziDialogUtils.getDialogNotTouchOutside(this, R.layout.dialog_confirm);
        TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
        TextView tvContent = (TextView)dialog. findViewById(R.id.tv_content);
        tvContent.setText("确认退出吗？");
        TextView tvNo = (TextView) dialog.findViewById(R.id.tv_no);
        TextView tvOk = (TextView) dialog.findViewById(R.id.tv_ok);

        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                getLoadingDialog().showDialog();
                RxTimerUtil.timer(500, new RxTimerUtil.IRxNext() {
                    @Override
                    public void doNext() {
                        getLoadingDialog().dismiss();
                        ToastUitl.showImageToastSuccess("退出登录成功");
                        if(EmptyUtils.isNotEmpty(getLoginDto())){
                            Set<String> set = new HashSet<>();
                            set.add("orgCode:"+getLoginDto().getOrgCode());
                            set.add("userId:"+getLoginDto().getUserId());
                            JPushInterface.deleteTags(SettingActivity.this,1001,set);
                        }

                        // PinziApplication.clearAllActivitys();
                        SPUtil.clearData(Constant.SP_VALUE.SP);
                        startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                        finish();
                        MyApplication.clearAllActivitys();
                    }
                });
            }
        });
        tvNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();



    }
}
